package cn.wolfcode.wolf2w.redis.service.impl;

import cn.wolfcode.wolf2w.domain.Travel;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.redis.service.ITravelStratisVoRedisService;
import cn.wolfcode.wolf2w.redis.util.RedisKeys;
import cn.wolfcode.wolf2w.redis.vo.TravelStatisVo;
import cn.wolfcode.wolf2w.service.ITravelService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.DateUtil;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class TravelStratisVoRedisServiceImpl implements ITravelStratisVoRedisService {
    @Autowired
    private ITravelService travelService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private IUserInfoService userInfoService;

    @Override
    public TravelStatisVo getTravelStatisVo(String tid) {
        // 拼接vo的key
        String key = RedisKeys.TRAVEL_STATIS_VO.join(tid);
        // 判断redis中是否存在key
        TravelStatisVo vo = new TravelStatisVo();
        if (redisTemplate.hasKey(key)){
            // 存在，获取vo
            String value = redisTemplate.opsForValue().get(key);
            vo = JSON.parseObject(value,TravelStatisVo.class);
        }else {
            // 不存在，从数据库中获取，并设置到redis中
            Travel travel = travelService.get(tid);
            BeanUtils.copyProperties(travel,vo);
            vo.setTravelId(tid);
            redisTemplate.opsForValue().set(key,JSON.toJSONString(vo));
        }
        return vo;
    }

    @Override
    public void setTravelStatisVo(TravelStatisVo vo) {
        // 拼接vo的key
        String key = RedisKeys.TRAVEL_STATIS_VO.join(vo.getTravelId());
        redisTemplate.opsForValue().set(key,JSON.toJSONString(vo));
    }

    @Override
    public void viewnumIncrease(String tid, int num) {
        // 获取vo
        TravelStatisVo vo = this.getTravelStatisVo(tid);
        // 阅读量+1
        vo.setViewnum(vo.getViewnum() + num);
        // 设置或更新到redis中
        this.setTravelStatisVo(vo);
    }

    @Override
    public void replynumIncrease(String travelId, int num) {
        // 获取vo
        TravelStatisVo vo = this.getTravelStatisVo(travelId);
        // 评论数 +1
        vo.setReplynum(vo.getReplynum() + num);
        // 设置或更新到redis中
        this.setTravelStatisVo(vo);
    }

    @Override
    public boolean favornumIncrease(String tid, String uid) {
        List<String> travelFavorList = this.getFavorList(uid);

        // 判断游记id是否存在 收藏集合list
        TravelStatisVo vo = this.getTravelStatisVo(tid);
        if (travelFavorList.contains(tid)){
            // 存在，做取消收藏操作，收藏数-1
            vo.setFavornum(vo.getFavornum() - 1);
            travelFavorList.remove(tid);
        }else {
            // 存在，做添加收藏操作，收藏数+1
            vo.setFavornum(vo.getFavornum() + 1);
            travelFavorList.add(tid);
        }

        // 更新vo和list（redis）
        this.setTravelStatisVo(vo);
        this.setFavorList(uid,travelFavorList);
        return travelFavorList.contains(tid);
    }

    @Override
    public List<String> getFavorList(String userId) {
        // 拼接key
        String key = RedisKeys.USER_TRAVEL_FAVOR.join(userId);
        // 判断key是否存在，获取游记收藏集合list
        List<String> travelFavorList = new ArrayList<>();
        if (redisTemplate.hasKey(key)){
            // 存在，获取游记收藏集合
            String value = redisTemplate.opsForValue().get(key);
            travelFavorList = JSON.parseArray(value,String.class);
        }else {
            // 不存在，从数据库获取初始化，并设置到redis中
            UserInfo userInfo = userInfoService.get(userId);
            if (userInfo != null) {
                travelFavorList = userInfo.getTravelFavors();
                redisTemplate.opsForValue().set(key, JSON.toJSONString(travelFavorList));
            }
        }
        return travelFavorList;
    }

    @Override
    public void setFavorList(String uid,List<String> favorList) {
        // 拼接key
        String key = RedisKeys.USER_TRAVEL_FAVOR.join(uid);
        // 设置到redis中
        redisTemplate.opsForValue().set(key,JSON.toJSONString(favorList));
    }

    @Override
    public boolean thumbupnumIncrease(String tid, String id) {
        // 拼接key
        String key = RedisKeys.USER_TRAVEL_THUMBUP.join(id, tid);
        // 判断key是否存在，不存在，创建key并设置到redis中，同时设置过期时间，点赞数加1 ，并同步更新vo；存在，不做任何处理，返回false
        if (!redisTemplate.hasKey(key)){
            Date endDate = DateUtil.getEndDate(new Date());
            Date beginDate = new Date();
            redisTemplate.opsForValue().set(key,"1",DateUtil.getDateBetween(beginDate,endDate), TimeUnit.SECONDS);

            TravelStatisVo vo = this.getTravelStatisVo(tid);
            vo.setThumbsupnum(vo.getThumbsupnum() + 1);
            this.setTravelStatisVo(vo);
            return true;
        }
        return false;
    }
}
